package org.openfidias.jdbase.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import org.openfidias.jdbase.db.DbManager;
import org.openfidias.jdbase.db.IRegister;
import org.openfidias.jdbase.locale.Message;

/**
 *
 * @author atila
 */
public class Do {

   public static void create(IRegister register, String table, String deletedTable)
           throws IOException, Exception {
      try {
         File f = DbManager.getTable(table);

         Long pointer = DbManager.getPointer(deletedTable);
         if (pointer != null) {
            RandomAccessFile file = new RandomAccessFile(f, "rw");
            file.seek(pointer);
            file.writeBytes(register.toLine());
            file.close();
         } else {
            PrintWriter writer = new PrintWriter(new FileOutputStream(f, true));
            writer.println(register.toLine());
            writer.close();
         }
      } catch (IOException ex) {
         throw new IOException(Message.tableNotFound(table));
      } catch (Exception e) {
         throw new Exception(e);
      }
   }

   public static IRegister read(IRegister register, String table) throws IOException, Exception {
      try {
         //IRegister search = null;
         File f = DbManager.getTable(table);
         BufferedReader reader = new BufferedReader(new FileReader(f));
         String line = reader.readLine();
         while (line != null) {
            if (register.equals(line)) {
               register = register.fromLine(line);
               line = null;
               continue;
            }
            line = reader.readLine();
         }
         reader.close();
         return register;
      } catch (IOException e) {
         throw new IOException(Message.tableNotFound(table));
      } catch (Exception e) {
         throw new Exception(e);
      }
   }

   public static boolean delete(IRegister register, String table, String deletedTable)
           throws IOException, Exception {
      try {
         boolean del = false;

         File f = DbManager.getTable(table);
         RandomAccessFile file = new RandomAccessFile(f, "rw");
         long pointer = file.getFilePointer();
         String line = file.readLine();

         while (line != null) {

            if (register.equals(line)) {
               file.seek(pointer);
               file.write("1".getBytes());
               // save pointer to file deletedFile
               DbManager.logicalDelete(deletedTable, pointer);
               del = true;
               line = null;
               continue;
            }
            pointer = file.getFilePointer();
            line = file.readLine();

         }
         file.close();
         return del;
      } catch (IOException iOException) {
         throw new IOException(Message.tableNotFound(table));
      } catch (Exception e) {
         throw new Exception(e);
      }
   }

   public static boolean exists(IRegister register, String table) throws IOException, Exception {
      try {
         boolean exists = false;

         File f = DbManager.getTable(table);
         BufferedReader reader = new BufferedReader(new FileReader(f));
         String line = reader.readLine();
         while (line != null) {
            if (register.equals(line)) {
               exists = true;
               line = null;
               continue;
            }
            line = reader.readLine();
         }
         reader.close();
         return exists;
      } catch (IOException e) {
         throw new IOException(Message.tableNotFound(table));
      } catch (Exception e) {
         throw new Exception(e);
      }
   }

   /**
    * Verifica se existe e retorna o ponteiro, caso contrário retorna null.
    * TODO: testing ... :-)
    * @param register
    * @param table
    * @return
    * @throws IOException
    * @throws Exception
    */
   public static Long existsPointer(IRegister register, String table) throws IOException, Exception {
      try {
         Long pointer = null;

         File f = DbManager.getTable(table);
         RandomAccessFile file = new RandomAccessFile(f, "rw");
         long p = file.getFilePointer();
         String line = file.readLine();

         while (line != null) {
            if (register.equals(line)) {
               pointer = p;
               line = null;
               continue;
            }
            p = file.getFilePointer();
            line = file.readLine();
         }
         file.close();
         return pointer;
      } catch (IOException e) {
         throw new IOException(Message.tableNotFound(table));
      } catch (Exception e) {
         throw new Exception(e);
      }
   }
}
